package com.fw.dao.impl;

import java.util.List;

import com.fw.dao.UserDao;
import com.fw.model.Permission;
import com.fw.model.Role;
import com.fw.model.User;
import com.fw.model.UserRole;
import com.fw.util.MD5Utils;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class UserDaoImpl implements UserDao {

	@Override
	public User query_userByUNameAndUPwdDao(User user) {
		String pwd = MD5Utils.getInstance().getStringHash(user.get("u_pwd").toString());
		List<User> us = User.userDao.find("select * from user u where u.u_email = ? and u.u_pwd = ? and u.u_status = ?",user.get("u_email"),pwd,1);
		Integer size = 0;
		size = us.size();
		if(size > 0)
			return us.get(0);
		return null;
	}

	@Override
	public Integer query_totalUserDao(String sql) {
		String s = "select count(*) from user where 1 = 1 "+ sql;
		long count = Db.queryLong(s);
		return (int) count;
	}

	@Override
	public List<User> query_allUserByPageDao(String sql, int firstPage,int maxPage) {
		String s = "select * from user u where 1 = 1 limit ?,?";
		List<User> us = User.userDao.find(s,firstPage,maxPage);
		if(us.size()>0)
			return us;
		return null;
	}

	@Override
	public boolean delete_userByIdDao(User user) {
		boolean flag = Db.deleteById("user", "u_id", user.getInt("u_id"));
		return flag;
	}

	@Override
	public boolean add_userDao(User user) {
		boolean flag = user.save();
		return flag;
	}
	@Override
	public User query_userByEmailDao(User user) {
		String s = "select * from user u where u.u_email = ?";
		List<User> us = User.userDao.find(s,user.getStr("u_email"));
		if(us.size()>0)
			return us.get(0);
		return null;
	}

	@Override
	public boolean update_userByIdDao(User user) {
		Record u = Db.findById("user", "u_id", user.getInt("u_id")).set("u_email", user.getStr("u_email")).set("u_name", user.getStr("u_name"))
				.set("u_status", user.getInt("u_status"));
		return Db.update("user","u_id",u);
	}

	@Override
	public User query_userByIdDao(User user) {
		String sql = "select * from user u where u.u_id = ?";
		List<User> us = User.userDao.find(sql, user.getInt("u_id"));
		if(us != null && us.size()>0) return us.get(0);
		return null;
	}

	@Override
	public Role query_roleByUserDao(User user) {
		List<Role> rs = User.userDao.getRoles();
		if(rs != null) return rs.get(0);
		return null;
	}

	@Override
	public boolean update_userRoleDao(User user,Role role) {
		String query = "select * from user_role ur where ur.u_id = ?";
		List<UserRole> urs = Db.query(query, user.getInt("u_id"));
		if(urs.size()<=0){
			Record userRole = new Record().set("u_id", user.getInt("u_id")).set("r_id",role.getInt("r_id"));
			Db.save("user_role",userRole);
			return true;
		}else{
			String s = "update user_role ur set ur.r_id = ? where ur.u_id = ?";
			int count = Db.update(s,role.getInt("r_id"),user.getInt("u_id"));
			if(count >0) return true;
			return false;
		}
		
	}

	@Override
	public boolean userIsPermissionDao(User user, Permission permisson) {
		String sql = "select u.* from user u,role r,user_role ur,permission p,role_permission rp where u.u_id = ur.u_id and r.r_id = ur.r_id "+
					 "and r.r_id = rp.r_id and p.p_id = rp.p_id and p.p_url = ? and u.u_id = ?";
		List<User> us = Db.query(sql, permisson.getStr("p_url"),user.getInt("u_id"));
		if(us.size() > 0) return true;
		return false;
	}

	@Override
	public boolean update_user_pwdDao(User user) {
		String sql = "update user u set u.u_pwd = ? where u.u_id = ?";
		String newPwd = MD5Utils.getInstance().getStringHash(user.getStr("u_pwd"));
		int count = Db.update(sql, newPwd,user.getInt("u_id"));
		if(count > 0)return true;
		return false;
	}
}
